package com.bbc.action.product;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.bbc.base.BaseAction;
import com.bbc.entity.product.Product;
import com.bbc.entity.product.ProductQuery;
import com.bbc.entity.type.BbcType;
import com.bbc.service.product.ProductService;
import com.bbc.service.type.BbcTypeService;

public class ProductAction extends BaseAction {

	private static final long serialVersionUID = 1L; 
	private Product product;
	private ProductQuery query;
	private List<Product> products;
	@Autowired
	private transient ProductService productService;
	@Autowired
	private transient BbcTypeService bbcTypeService;
	
	private Map<Integer, List<BbcType>> typeMap = new HashMap<Integer, List<BbcType>>();
	
	public String addProductFromTbapi() {
		if (product == null) {
			product = new Product();
		}
		productService.addProductFromTBApi(product.getNumIid(),getU(),product.getShowType());
		return list();
	}
	
	public String list() {
		if (query == null) {
			query = new ProductQuery();
		}
		products = productService.findProduct(query, page, 20);
		return SUCCESS;
	}
	
	public String listt() {
		inittypelist();
		if (query == null) {
			query = new ProductQuery();
		}
		query.setOrderBy("type1");
		products = productService.findProduct(query, page, 20);
		return "ptlist";
	}
	
	public String detailUpdateProduct() {
		if (product!=null&&product.getId()!=null) {
			product = productService.findProductById(product.getId());
			return "detail";
		} else {
			return list();
		}
	}
	
	public String updateProduct() {
		productService.updateProduct(product);
		product = productService.findProductById(product.getId());
		return "detail";
	}
	
	private void inittypelist() {
		List<BbcType> list = bbcTypeService.findBbcTypeByParent(0);
		typeMap.put(0, list);
		for (BbcType bt:list) {
			typeMap.put(bt.getTypeId(), bbcTypeService.findBbcTypeByParent(bt.getTypeId()));
		}
	}
	
	public void getType() {
		int parent = Integer.valueOf(request.getParameter("parent"));
		List<BbcType> list = bbcTypeService.findBbcTypeByParent(parent);
		StringBuilder r = new StringBuilder();
		r.append("[");
		int i = list.size();
		for (BbcType bt : list) {
			i--;
			r.append("{\"id\":\"").append(bt.getTypeId()).append("\",");
			r.append("\"name\":\"").append(StringUtils.defaultIfEmpty(bt.getTypeName(), "") ).append("\"}");			
			if (i != 0) {
				r.append(",");
			}
		}
		r.append("]");
		try {
			sendMessage(r.toString());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void updatePType() {
		int id = Integer.valueOf(request.getParameter("id"));
		int type1 = Integer.valueOf(request.getParameter("type1"));
		int type2 = Integer.valueOf(request.getParameter("type2"));
		Product p = new Product();
		p.setId(id);
		p.setType1(type1);
		p.setType2(type2);
		try {
			productService.updateProduct(p);
			sendMessage("[{\"result\":\"true\"}]");
		} catch (Exception e) {
			try {
				sendMessage("[{\"result\":\"true\"}]");
			} catch (Exception e1) {
				e1.printStackTrace();
			}
		}
		
	}

	public Product getProduct() {
		return product;
	}

	public void setProduct(Product product) {
		this.product = product;
	}

	public ProductQuery getQuery() {
		return query;
	}

	public void setQuery(ProductQuery query) {
		this.query = query;
	}

	public List<Product> getProducts() {
		return products;
	}

	public void setProducts(List<Product> products) {
		this.products = products;
	}

	public Map<Integer, List<BbcType>> getTypeMap() {
		return typeMap;
	}

	public void setTypeMap(Map<Integer, List<BbcType>> typeMap) {
		this.typeMap = typeMap;
	}

}
